
import { AES, enc, mode, pad } from 'crypto-js'
import { getRandomNumberArr, u8arr_to_b64 } from './lib'

class AESCryptoTool {
  #key: string
  constructor(key?: string) {
    this.#key = key || u8arr_to_b64(getRandomNumberArr())
  }
  encrypto(content: any, addTimestamp: boolean = false) {
    const strType = (typeof content === 'string')
    if (!strType && addTimestamp) {
      Object.assign(content, { t: Date.now() })
    }
    const ct = strType ? content : JSON.stringify(content)
    const cp = AES.encrypt(enc.Utf8.parse(ct), enc.Base64.parse(this.#key), { mode: mode.ECB, padding: pad.Pkcs7 })
    return cp.ciphertext.toString(enc.Base64)
  }
  decrypto(ciphertext?: string) {
    if (!ciphertext) return
    const wr = AES.decrypt(ciphertext, enc.Base64.parse(this.#key), { mode: mode.ECB, padding: pad.Pkcs7 })
    return (wr).toString(enc.Utf8)
  }

  updateKey(key?: string | false) {
    this.#key = key || u8arr_to_b64(getRandomNumberArr())
  }
}


class AesTester {

  async begin() { return this }
  async generateTest() {
    console.log('=======================================================>');
    console.log('生成key测试');
    const aesTool = new AESCryptoTool()
    const originalData = { name: '家铭' }
    console.log('aes:', aesTool);

    console.log('开始加密对象....', originalData);
    const ObjEcryptoResult = aesTool.encrypto(originalData)
    console.log('对象加密结果:', ObjEcryptoResult);
    console.log('开始解密对象....');
    const objDecryptoResult = aesTool.decrypto(ObjEcryptoResult)
    console.log('对象密文解密结果:', objDecryptoResult, JSON.parse(objDecryptoResult!));
    console.log('<=======================================================');
    return this
  }
  async ImportTest() {
    const tempKey = '54LoboWxePsPRJ0Me2Icsw=='
    console.log('=======================================================>');
    console.log('导入外部key测试');
    const aesTool = new AESCryptoTool(tempKey)
    const originalData = { name: '家铭' }
    console.log('开始加密对象....', originalData);
    const ObjEcryptoResult = aesTool.encrypto(originalData)
    console.log('对象加密结果:', ObjEcryptoResult);
    console.log('开始解密对象....');
    const objDecryptoResult = aesTool.decrypto(ObjEcryptoResult)
    console.log('对象密文解密结果:', objDecryptoResult, JSON.parse(objDecryptoResult!));
    console.log('<=======================================================');
    return this
  }
  async CiTextDecodeTest() {
    const tempKey = '54LoboWxePsPRJ0Me2Icsw=='
    const ciText = 'zcz5SOCkOD9DXyiki8ukzTLTbTemqE329PWiadUtRas='
    console.log('=======================================================>');
    console.log('导入外部key和密文测试');
    console.log('密文:', ciText);
    const aesTool = new AESCryptoTool(tempKey)
    console.log('开始解密对象....');
    const strDecryptoResult = aesTool.decrypto(ciText)
    console.log('对象密文解密结果:', strDecryptoResult, JSON.parse(strDecryptoResult!));
    console.log('<=======================================================');
    return this
  }
  async serverCiphertextTest() {
    const serverAesKey = 'BkrFVkZW5txZniBZJPJdlQ==',
      serverCiphertext = 'ZMhW8VzZHNw0dGzo18REOnbv9jt/rQCBB8l1XMdxoww='
    console.log('=======================================================>');
    console.log('导入server key和密文测试');
    console.log('密文:', serverCiphertext);
    const aesTool = new AESCryptoTool(serverAesKey)
    console.log('开始解密对象....');
    const strDecryptoResult = aesTool.decrypto(serverCiphertext)
    console.log('对象密文解密结果:', strDecryptoResult);
    console.log('<=======================================================');
    return this
  }
}
export { AESCryptoTool, AesTester }